<template>
  <div class="login">
    <el-form
      :model="loginForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="160px"
      class="loginform"
    >
      <h1>系统登录</h1>
      <el-form-item label="用户名" prop="account">
        <el-input
          v-model="loginForm.account"
          prefix-icon="iconfont icon-yonghuming"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          style="width: 300px"
          v-model="loginForm.password"
          @click.native="changeType"
          prefix-icon="iconfont icon-mima"
          :type="isClose ? 'password' : 'text'"
          :suffix-icon="
            isClose
              ? 'iconfont icon-yanjing_bi'
              : 'iconfont icon-yanjing-zhengyan'
          "
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkLogin } from "@/apis/acc.js";
import local from "@/utilis/local.js";
export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      isClose: true,
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async (vali) => {
        if (vali) {
          let { code, token } = await checkLogin(this.loginForm);
          if (code == 0) {
            local.setItem("tk", token);
            this.$router.push("/");
          }
        }
      });
    },
    changeType(e) {
      if (e.target.className.includes("icon-yanjing")) {
        this.isClose = !this.isClose;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    h1 {
      text-align: center;
      color: #fff;
      margin-left: 35%;
      margin-bottom: 20px;
    }
    .loginform {
      width: 600px;
      /deep/.el-form-item__label {
        color: #fff;
      }
      /deep/.el-input__inner {
        background-color: #283443;
      }
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>